package components.lifes;

import java.awt.Color;
import java.awt.Font;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.GameComponent;
import com.uqbar.vainilla.appearances.Label;
import components.scenes.ScoreableGameScene;

public class LifeCounter extends GameComponent<ScoreableGameScene> {

	private Integer lifes;
	private String showText;
	private Label label;
	private boolean changed;
	
	public LifeCounter(int initialLifes){
		this.setX(0);
		this.setY(0);
		this.setLifes(initialLifes);
		this.changed = false;
		this.showText = "Vidas: ";
		this.label = new Label(new Font(Font.SERIF, Font.BOLD, 20), Color.WHITE, this.getShowText());
		this.setAppearance(this.label);
	}
	
	private String getShowText() {
		return this.showText + this.getLifes();
	}

	public void update(DeltaState deltaState) {
		super.update(deltaState);
		
		if (this.hasChanged()){
			
			if (this.lifes == 0){
				this.label.setColor(Color.RED);
			}

			this.label.setText(this.getShowText());
			
			this.setChanged(false);
		}

	}

	public void loseLife(){
		if (this.getLifes() == 0){
			this.getScene().gameOver();
		}
		else{
			this.lifes--;
			this.setChanged(true);
		}
		
	}
	public void gainLife(){
		this.lifes++;
		this.label.setColor(Color.BLACK);
		this.setChanged(true);
	}
	public int getLifes() {
		return lifes;
	}

	public void setLifes(int lifes) {
		this.lifes = lifes;
	}

	public void setChanged(boolean changed) {
		this.changed = changed;
	}

	public boolean hasChanged() {
		return changed;
	}

}
